package com.wbao.dianniu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.ClearEditText;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 编辑昵称
 *
 * @author yejianjun
 */
public class NiceNameEdit extends BaseActivity {

    private ClearEditText mEditText;
    //    private ImageButton deleteButton;
    private Button rightBtn;
//    private final int MAX_COUNT = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.nice_name_edit);
        setTitleName(getResources().getString(R.string.edit_niceName));
        mEditText = findViewById(R.id.niceNameET);
//        deleteButton = (ImageButton) findViewById(R.id.niceNameDelete);
//        deleteButton.setOnClickListener(this);
        String result = getIntent().getStringExtra(Const.INTENT_NICENAME);
        if (!TextUtils.isEmpty(result)) {
            mEditText.setText(result);
        }
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.save));
        rightBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String niceName = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(niceName)) {
                    Notification.toast(NiceNameEdit.this, "昵称不能为空");
                    return;
                }
                if (niceName.contains("电纽") || niceName.contains("电钮") || niceName.contains("电牛") || niceName.contains("电妞") || niceName.contains("电扭")) {
                    Notification.toast(NiceNameEdit.this, "昵称中请勿包含系统关键字");
                    return;
                }
                /*if (!checkContent(niceName)) {
                    Notification.toast(NiceNameEdit.this, "名字只允许中英文数字以及下划线");
                    return;
                }*/
                Intent intent = getIntent();
                // 把返回数据存入Intent
                intent.putExtra(Const.NICE_NAME_INTENT, mEditText.getText().toString().trim());
                // 设置返回数据
                setResult(RESULT_OK, intent);
                NiceNameEdit.this.finish();
            }
        });
//        Utils.ContentLengthLimit(this, mEditText, MAX_COUNT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.niceNameDelete:
//                mEditText.setText("");
//                break;
            case R.id.back_button:
                this.finish();
                break;
            /*case R.id.right_button:    //保存
                String niceName = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(niceName)) {
                    Notification.toast(this, "昵称不能为空");
                    return;
                }
                if (niceName.contains("电纽") || niceName.contains("电钮") || niceName.contains("电牛") || niceName.contains("电妞") || niceName.contains("电扭")) {
                    Notification.toast(this, "昵称中请勿包含系统关键字");
                    return;
                }
                if (!checkContent(niceName)) {
                    Notification.toast(this, "名字只允许中英文数字以及下划线");
                    return;
                }
                Intent intent = getIntent();
                // 把返回数据存入Intent
                intent.putExtra(Const.NICE_NAME_INTENT, mEditText.getText().toString().trim());
                // 设置返回数据
                setResult(RESULT_OK, intent);
                this.finish();
                break;*/
            default:
                break;
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 名字只允许中英文数字以及下划线
     */
    public boolean checkContent(String content) {
        boolean ret;
        String mat = "^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$";
        Pattern pattern = Pattern.compile(mat);
        Matcher matcher = pattern.matcher(content);
        ret = matcher.matches();
        return ret;
    }

}
